﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.Framework.Translate;
using CrmContacto.Contracts.Contracts;
using Web.Framework.Mapper;
using Web.CrmContacto.Code.Security;

namespace CrmContacto.WebApp.App
{
    public partial class AccionDeMarketingComunicacionTipoEdit : BasePage
    {
        public AccionDeMarketingComunicacionTipo tipoCurrent
        {
            get
            {
                return (AccionDeMarketingComunicacionTipo)ViewState["AccionDeMarketingComunicacionTipo"];
            }
            set
            {
                ViewState["AccionDeMarketingComunicacionTipo"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton eliminarButton = new LinkButton();
        LinkButton volverButton = new LinkButton();
        LinkButton guardarButton = new LinkButton();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            
            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);
            
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.CausesValidation = false;
            volverButton.ID = "BoCommandVolver";
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            volverButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(volverButton);
                        
            eliminarButton.ID = "BoCommandEliminar";
            eliminarButton.CausesValidation = false;
            eliminarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            eliminarButton.Click += new EventHandler(eliminarButton_Click);
            actionButtons.Add(eliminarButton);

            base.LoadContextActions(actionButtons);
        }

        private void eliminarButton_Click(object sender, EventArgs e)
        {
            try
            {
                WebServiceHelper.EliminarMarketingComunicacionTipo(tipoCurrent.Id);
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlAccionDeMarketingComunicacionTipoList + string.Format("?{0}=1", MiscClass.QueryShowDeleted), true);

                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Message.RegistroEliminado"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            try
            {
                HasPermissionToSeeMe(CrmSecurity.MANAGE_MKT_COMMUNICATIONS_TYPES);

                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["Id"] == null)
                    {
                        tipoCurrent = new AccionDeMarketingComunicacionTipo();
                        tipoCurrent.EmpresaId = BasePage.CompanyId;
                    }
                    else
                    {
                        TipoAccionDeMarketingComunicacionSearch search = new TipoAccionDeMarketingComunicacionSearch();
                        search.EmpresaId = BaseWebPage.CompanyId;
                        search.Id = Convert.ToInt32(this.Request.QueryString["Id"]);
                        tipoCurrent = WebServiceHelper.GetMarketingComunicacionTipo(search).First();
                    }

                    LoadCbos();
                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void LoadCbos()
        {
            ConfiguracionAccionMarketingSearch search = new ConfiguracionAccionMarketingSearch();
            search.EmpresaId = BasePage.CompanyId;
            var configuraciones = WebServiceHelper.GetAccionesMarketingConfiguracion(search);
            UIHelper.LoadCbo(configuraciones, cboConfiguracionId, string.Empty);
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();

            eliminarButton.Visible = tipoCurrent.Id != 0;

            if (tipoCurrent.Id != 0)
            {
                SetJavascriptForDeleteRecord(eliminarButton, tipoCurrent.Nombre);
            }

            //this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {           
            EntityMapper.Map(tipoCurrent, this);
        }

        private void SetData()
        {
            EntityMapper.Map(this, tipoCurrent);
        }

        private void Save()
        {
            tipoCurrent = WebServiceHelper.SaveMarketingComunicacionTipo(tipoCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlAccionDeMarketingComunicacionTipoList + string.Format("?{0}=1", MiscClass.QueryShowSaveOk), true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkIrAConfiguracion_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.cboConfiguracionId.SelectedIndex >= 0)
                {
                    string configId = this.cboConfiguracionId.SelectedValue;
                    NavigationContext.RedirectTo(MiscClass.UrlMarketingConfiguracionEdit, string.Format("?Id={0}", configId));
                }
                else
                {
                    NavigationContext.RedirectTo(MiscClass.UrlMarketingConfiguracionList, string.Empty);
                }                               
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
        
        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlAccionDeMarketingComunicacionTipoList, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}